package CelestialDataAccess;

import javax.sql.rowset.*; // provides cached row set interface
import java.sql.*;         // provides connection object and sql exception

public class UsersDataAccess extends DataAccessBase {

    public static final String PASSWORD = "Password";
    public static final String USR_LEVEL = "UserLevel";
    public static final String FIRST_NAME = "FirstName";
    public static final String LAST_NAME = "LastName";
    public static final String EMAIL = "Email";
    /*************************************************************************/
    /* Client methods */
    public User validateLogin(String email, String password) throws DataAccessException{

        CallableStatement stm = DataAccessBase.prepareStatement(
                                "{Call uspValidateLogin(?,?)}");
        CachedRowSet crs;

        try{
            stm.setString(EMAIL, email);
            stm.setBytes(PASSWORD, CryptService.encrypt(password));

            crs = getData(stm);
            stm.close();
            
            if(!crs.last()) return null;

            crs.last();
            if(crs.getInt("UserLevel")==1)
                return (new CustomersDataAccess().getCustomerByEmail(crs.getString(EMAIL)));
            else
                return (new EmployeesDataAccess().getEmployeeByEmail(crs.getString(EMAIL)));

        }catch(SQLException ex){
            throw new DataAccessException(ex.getMessage());
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage());
        }
    }
    /*----------------------------------------------------------------------*/
    public boolean isEmailRegistered(String email) throws DataAccessException{

        CallableStatement stm = prepareStatement("{Call uspIsEmailRegistered(?)}");
        boolean bEmailRegistered;
        try{
            stm.setString(EMAIL, email);
            bEmailRegistered =  getData(stm).last();
            stm.close();
        }catch(SQLException ex){
            throw new DataAccessException(ex.getMessage());
        }
        return bEmailRegistered;
    }
    /*************************************************************************/
    public boolean isAccountActive(String email) throws DataAccessException{
        
        CallableStatement stm = prepareStatement("{Call uspIsAccountActive(?)}");
        boolean bAccountActive;
        try{
            stm.setString(EMAIL, email);
            bAccountActive = getData(stm).last();
            stm.close();
        }catch(SQLException ex){
            throw new DataAccessException(ex.getMessage());
        }
        return bAccountActive;
    }
    /*************************************************************************/
    public boolean isEmailTaken(int id, String email) throws DataAccessException{

        CallableStatement stm = prepareStatement("{CALL uspIsEmailTaken(?,?)}");
        boolean bEmailTaken;
        try{
            stm.setString(EMAIL, email);
            stm.setInt("ID", id);

            bEmailTaken = getData(stm).last();
            stm.close();
        }catch(SQLException ex){
            throw new DataAccessException(ex.getMessage());
        }
        return bEmailTaken;
    }
    /*************************************************************************/
    /* test validateLogin */
    public static void main(String[] args){

       UsersDataAccess daUsers = new UsersDataAccess();
       User usr;

       try{
           System.out.println(daUsers.isAccountActive("chseng@homecodeworks.com"));
           usr = daUsers.validateLogin("chseng.acad@gmail.com", "1123581321");
           System.out.println(daUsers.isEmailRegistered("chseng@hecodeworks.com"));
           if(usr!=null){
           System.out.println(usr.getEmail() + "\t"
                       + usr.getPassword() + "\t"
                       + usr.getFirstName() + "\t"
                       + usr.getUserLevel());
            }
       }catch(DataAccessException ex){
           System.out.println(ex.getMessage());
       }
   }
}
